<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    
  </style>
</head>
<body>
  <input type="text" class="txt">
  <ul class="list">
  </ul>
</body>
<script>

  const txt = document.querySelector(".txt");
  const list = document.querySelector(".list");

  txt.oninput = function(){

    if(this.value === ""){
      list.innerHTML = "";
      return;
    }

    jsonp({
      url:"https://www.baidu.com/sugrec",
      data:{
        prod: "pc",
        wd: this.value,
        cb: "sfsgdgh"
      },
      success:res=>{
        let str = "";
        res.g.forEach(val=>{
          str += `<li>${val.q}</li>`
        })
        list.innerHTML = str;
      },
      jsonpName:"cb"
    })
  }

  function jsonp( { url, data, success, jsonpName } ){
    let str = "";
    for(let i in data){
      str += `${i}=${data[i]}&`
    }
    str = str.slice(0,-1);
    url += "?" + str;

    const script = document.createElement("script")
    script.src = url;
    document.body.appendChild(script);

    window[data[jsonpName]] = function(res){
      success(res);
    }
  }



  
</script>
</html>